



<template>

  <el-card shadow="always" :body-style="{ padding: '20px' }">
    <el-form :inline="true" class="demo-form-inline" :disabled="disabled">
      <el-form-item label="一级分类">
        <el-select placeholder="请选择" v-model="(category1Id as number)" @change="category1Change">
          <el-option v-for="item in categoryList1" :label="item.name" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类">
        <el-select placeholder="请选择" v-model="(category2Id as number)" @change="category2Change">
          <el-option v-for="item in categoryList2" :label="item.name" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类">
        <el-select placeholder="请选择" v-model="(category3Id as number)" @change="category3Change">
          <el-option v-for="item in categoryList3" :label="item.name" :value="item.id" :key="item.id" />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>

</template>

<script lang="ts">

import { defineComponent } from 'vue';
export default defineComponent({
  name: 'Category',
});
</script>
s
<script setup lang="ts">

// import type { reqCategoryType, reqCategoryItemType } from "@/api/attr";
import { onMounted } from "vue";
import { storeToRefs } from "pinia";
//引入attr的仓库
import useAttrStore from "@/stores/attr";
//获取attr仓库实例对象
const attrStore = useAttrStore();
//接受attr仓库中的state数据
const { categoryList1, category1Id, categoryList2, category2Id, categoryList3, category3Id, } = storeToRefs(attrStore);

//初始化操作
onMounted(() => {
  //调用store中的方法,发送1级分类请求
  attrStore.getCategory1();
});

//一级分类被选中后，刷新二级请求
const category1Change = () => {
  /* 选中一级列表清除 二级和三级内容 */
  categoryList2.value = [],
    category2Id.value = null,
    categoryList3.value = [],
    category3Id.value = null,
    //调用store中的方法,发送2级分类请求
    attrStore.getCategory2();
}

//二级分类被选择后,处理三级分类请求
const category2Change = () => {
  /* 选中二级列表 清空 三级 */
  categoryList3.value = [],
    category3Id.value = null,
    //调用store中的方法,发送3级分类请求
    attrStore.getCategory3();
};

//三级列表被选中
const category3Change = () => {

}

//修改和新增属性 三级联动禁用
defineProps<{
  disabled: boolean;
}>()

</script>